package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface UserDao_lzl {
    /*
    * 根据用户名查询USER对象
    * */
    @Select("select * from t_user where username=#{value}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "gender",column = "gender"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "remark",column = "remark"),
            @Result(property = "station",column = "station"),
            @Result(property = "telephone",column = "telephone"),

            @Result(
                    property = "roles",
                    column = "id",
                    javaType = Set.class,
                    many = @Many(select = "com.itheima.dao.RoleDao_lzl.findRoleByUserId")
            )
    })
    User findUser(String username);


    /*
     * 根據條件分頁查詢
     * */
    @Select("<script> select id,username,gender,DATE_FORMAT( birthday, '%Y-%m-%d' ) AS birthday,telephone,remark from t_user"+
            "<where>"+
            "<if test='value!=null and value.length>0'> and username=#{value} </if>"+
            "<if test='value!=null and value.length>0'> or telephone=#{value} </if>"+
            "</where>"+
            "</script>"
    )
    Page<Map> findPageByCondition(String queryString);

    /*
     * 添加到t_checkgroup，并返回主键id
     * */
    @Insert("insert into t_user values(#{id},#{birthday},#{gender},#{username},#{password},#{remark},#{station},#{telephone})")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Integer.class)
    Integer add(User user);
    /*
     * 添加到关联表t_checkgroup_checkitem
     * */
    @Insert("insert into t_user_role values(#{user_id},#{role_id})")
    void addCheckGroupIdAndCheckitemId(Map<String, Integer> map);


    /*
     * 根据id，查询用户
     * */
    @Select("select * from t_user where id=#{id}")
    User findById(Integer id);


    /*
     * 根据id，查询关联表，并返回roleIDS
     * */
    @Select("select role_id from t_user_role where user_id=#{id}")
    List<Integer> findroleIdsByuserId(Integer id);

    /*
     * 根据userid，删除关联表中的roleid
     * */
    @Delete("delete from t_user_role where user_id=#{userid}")
    void delete(Integer userid);


    /*
     * 修改user
     * */
    @Update("<script> update t_user"+
            "<set>"+
            "<if test='birthday!=null'> birthday=#{birthday},</if>"+
            "<if test='gender!=null'> gender=#{gender},</if>"+
            "<if test='username!=null'> username=#{username},</if>"+
            "<if test='password!=null'> password=#{password},</if>"+
            "<if test='remark!=null'> remark=#{remark},</if>"+
            "<if test='telephone!=null'> telephone=#{telephone},</if>"+
            "</set>"+
            "where id = #{id}"+
            "</script>"
    )
    void update(User user);

    /*
     * 根据userid。查询关联表数据的出现的次数
     * */
    @Select("select count(*) from  t_user_role where user_id=#{userid}")
    int findCountByuserId(int userid);


    /*
     * 根据id，删除user对象
     * */
    @Delete("delete from t_user where id=#{userid}")
    void deleteUser(int userid);
    /*
     * 查询所有，进行回显到套餐管理
     * */
    @Select("select * from t_user")
    List<User> findAll();
}
